package com.hxjr.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

public class CustomContentDialog extends Dialog {
	private Context mContext = null;

    private TextView mTvTitle;
    private FrameLayout mContent;
    private TextView mTvCancel;
    private TextView mTvConfirm;

    private View.OnClickListener mCancelListener;
    private View.OnClickListener mConfirmListener;

	public CustomContentDialog(Context context, int theme)
	{
		super(context, theme);
		this.mContext = context;
		initWindowAttributes();
	}

	public CustomContentDialog(Context context)
	{
		super(context);
		this.mContext = context;
		initWindowAttributes();
	}

    public CustomContentDialog(Context context, boolean cancelable,
                               OnCancelListener cancelListener)
    {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        initWindowAttributes();
    }

	private void initWindowAttributes()
	{
		getWindow().setWindowAnimations(R.style.AnimBottom);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setBackgroundDrawableResource(android.R.color.transparent);

		// setTitle(R.string.please_choose_car);
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
		getWindow().setAttributes(lp);
		getWindow().setGravity(Gravity.CENTER);
		setCanceledOnTouchOutside(true);

	}


	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_content_dialog);

        initView();

	}

    private void initView() {
        mTvTitle = (TextView) findViewById(R.id.tv_dialog_title);
        mContent = (FrameLayout) findViewById(R.id.fl_dialog_content);
        mTvCancel = (TextView) findViewById(R.id.tv_dialog_cancel);
        mTvConfirm = (TextView) findViewById(R.id.tv_dialog_confirm);

    }


    public void setTitle(String title){
        mTvTitle.setText(title);
    }

    public String getTitle(){
        if(mTvTitle != null){
            return mTvTitle.getText().toString();
        }
        return null;
    }

    public void setContent(View view){
        mContent.removeAllViews();
        mContent.addView(view);
    }

    public void setCancelListener(View.OnClickListener cancelListener) {
        this.mTvCancel.setOnClickListener(cancelListener);
    }


    public void setConfirmListener(View.OnClickListener confirmListener) {
        this.mTvConfirm.setOnClickListener(confirmListener);
    }

    /**
     * 获取屏幕的大小
     *
     * @return
     */
    public DisplayMetrics getDisplayMetrics() {
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

}
